package com.example.system.controller.sysUser;


import com.example.common.core.constants.HttpConstants;
import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.Result;
import com.example.common.core.domain.vo.LoginUserVO;
import com.example.system.domain.sysUser.dto.LoginDTO;
import com.example.system.domain.sysUser.dto.SysUserSaveDTO;
import com.example.system.service.sysUser.ISysUserService;
import com.example.system.service.sysUser.impl.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/sysUser")
@Tag(name = "管理员登录接口")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private SysUserService sysUserService;

    //登录
    @Operation(summary = "新增管理员",description = "根据提供的信息新增管理员用户")
    @ApiResponse(responseCode = "1000",description = "登录成功")
    @ApiResponse(responseCode = "3102",description = "用户名不存在")
    @ApiResponse(responseCode = "3103",description = "用户名/密码错误")
    @PostMapping("/login")
    public Result<String> login(@Validated @RequestBody LoginDTO loginDTO){
        log.info("管理员登陆,loginDTO{}",loginDTO);
        return Result.ok(iSysUserService.login(loginDTO));
    }

    //新增管理员
    @Operation(summary = "新增管理员",description = "根据提供的信息新增管理员用户")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody SysUserSaveDTO sysUserSaveDTO){
        log.info("管理员添加,sysUserSaveDTO{}",sysUserSaveDTO);
        return toResult(iSysUserService.insertSysUser(sysUserSaveDTO));
    }

    //退出登陆
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return toResult(sysUserService.logout(token));
    }

    //获取当前用户信息
    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return sysUserService.info(token);
    }


}
